//
//  MapViewController.m
//  chatchatchat
//
//  Created by 小灰灰的pro on 16/3/23.
//  Copyright © 2016年 小灰灰. All rights reserved.
//

#import "MapViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface MapViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
@property (nonatomic,strong)CLLocationManager *locationManager;
@property (nonatomic,strong) MKMapView *currentMapVeiw;
@property (nonatomic,strong) UILabel *locationLabel;
@end

@implementation MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //布局
    [self initLayout];
    //初始化
    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager requestWhenInUseAuthorization];
    //设置代理对象
    self.locationManager.delegate = self;
    //开启定位
    [self.locationManager startUpdatingLocation];
    
    //设置地图类型
    self.currentMapVeiw.mapType = MKMapTypeStandard;
    //设置代理
    self.currentMapVeiw.delegate = self;
    //设置跟随状态
    self.currentMapVeiw.userTrackingMode = MKUserTrackingModeFollow;
    //显示当前位置
    self.currentMapVeiw.showsUserLocation = YES;
    [MKMapView initialize];
    
}

- (void)initLayout{
    self.currentMapVeiw = [[MKMapView alloc] initWithFrame:CGRectMake(0, 94, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 30)];
    [self.view addSubview:self.currentMapVeiw];
    self.locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 30)];
    self.locationLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.locationLabel];
}

#pragma mark --- CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    NSLog(@"定位成功");
    CLLocation *currentLocation = locations[0];
    
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //逆地理编码
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        CLPlacemark *placemark = placemarks[0];
        self.locationLabel.text = placemark.name;
        NSLog(@"%@",placemark);
    }];
    
    //关闭定位
    [self.locationManager stopUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"定位失败%@",error);
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    //自定义大头针
    MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc] init];
    
    //设置大头针经纬度
    pointAnnotation.coordinate = userLocation.coordinate;
    
    pointAnnotation.title = userLocation.title;
    //把大头针添加到地图上
    [mapView addAnnotation:pointAnnotation];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
